package edu.cuit.zhuyimeng.ticket.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.ticket.controller.vo.TicketInfoVO;
import edu.cuit.zhuyimeng.ticket.service.ITicketDispatchService;
import edu.cuit.zhuyimeng.ticket.service.ITicketSessionManager;
import edu.cuit.zhuyimeng.ticket.service.IUserTicketManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户购票相关
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("user")
@Tag(name = "用户购票相关")
public class TicketUserController {

    private final ITicketDispatchService ticketDispatchService;
    private final IUserTicketManager userTicketManager;
    private final ITicketSessionManager ticketSessionManager;

    /**
     * 购票
     * @return 购票结果
     */
    @PutMapping("/")
    @SaCheckLogin
    @Operation(summary = "购票",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> buyTicket(@RequestParam("flightId") @NotNull Long flightId) {
        ticketDispatchService.buyTicket(flightId);
        return CommonResult.success();
    }

    /**
     * 退票
     * @return 退票结果
     */
    @DeleteMapping("/")
    @SaCheckLogin
    @Operation(summary = "退票",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> refundTicket(@RequestParam("flightId") @NotNull Long flightId) {
        ticketDispatchService.refundTicket(flightId);
        return CommonResult.success();
    }

    /**
     * 查询用户飞机票
     * @return 购票记录
     */
    @GetMapping("/")
    @SaCheckLogin
    @Operation(summary = "查询用户飞机票",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<TicketInfoVO> queryTicket() {
        return CommonResult.success(userTicketManager.getUserTicketInfo(((String) StpUtil.getLoginId())));
    }

    /**
     * 查询航班是否售罄
     * @return 是否售罄
     */
    @GetMapping("/soldOut")
    @Operation(summary = "查询航班是否售罄")
    public CommonResult<Boolean> isSoldOut(@RequestParam("flightId") @NotNull Long flightId) {
        return CommonResult.success(ticketSessionManager.isSoldOut(flightId));
    }

}
